﻿Imports System.Data
Imports System.Configuration
Imports System.Collections.Generic
Imports System.Data.SqlClient
Imports TepcelDominio
Imports TepcelDAL.DBManager

Public Class GuiaRemisionDAL : Inherits DBManager
    Public Function NuevoGuiaRemision(ByRef guiaRemision As GuiaRemision) As Integer
        Dim comando As SqlCommand
        Dim i As Integer
        Dim j As Integer
        'Insertar Guía
        comando = MyBase.CreateSelectCommand("SPInsertGuiaRemision")
        comando.Parameters.Add("@codigoGuiaRemision", SqlDbType.Char, 8).Value = guiaRemision.codigoGuiaRemision
        comando.Parameters.Add("@serieGuiaremision", SqlDbType.Char, 3).Value = guiaRemision.serieGuiaremision
        comando.Parameters.Add("@numeroGuiaRemision", SqlDbType.Char, 6).Value = guiaRemision.numeroGuiaRemision
        comando.Parameters.Add("@direccionPartida", SqlDbType.VarChar, 200).Value = guiaRemision.direccionPartida
        comando.Parameters.Add("@direccionLlegada", SqlDbType.VarChar, 200).Value = guiaRemision.direccionLlegada
        comando.Parameters.Add("@fechaEmision", SqlDbType.DateTime).Value = guiaRemision.fechaEmision
        comando.Parameters.Add("@fechaInicioTraslado", SqlDbType.DateTime).Value = guiaRemision.fechaInicioTraslado
        comando.Parameters.Add("@codigoMotivo", SqlDbType.Char, 3).Value = guiaRemision.Motivo.codigoMotivo
        comando.Parameters.Add("@empresaDestino", SqlDbType.Char, 3).Value = guiaRemision.EmpresaDestino.codigoEmpresa
        comando.Parameters.Add("@empresaTransporte", SqlDbType.Char, 3).Value = guiaRemision.EmpresaTransporte.codigoEmpresa
        comando.Parameters.Add("@codigoUsuario", SqlDbType.Char, 4).Value = guiaRemision.codigoUsuario
        comando.Parameters.Add("@codigoNotaAlmacen", SqlDbType.Int).Value = guiaRemision.NotaAlmacen.codigoNotaAlmacen
        comando.Parameters.Add("@estadoGuiaRemision", SqlDbType.Char, 1).Value = guiaRemision.estadoGuiaRemision

        comando.Parameters("@codigoGuiaRemision").Direction = ParameterDirection.InputOutput

        If MyBase.ExecuteQuery(comando) > 0 Then
            guiaRemision.codigoGuiaRemision = comando.Parameters("@codigoGuiaRemision").Value
            'Insertar Detalle Guia
            For i = 0 To guiaRemision.DetalleGuia.Count - 1
                comando = MyBase.CreateSelectCommand("SPInsertDetalleGuiaRemision")
                comando.Parameters.Add("@codigoProducto", SqlDbType.Char, 4).Value = guiaRemision.DetalleGuia(i).Producto.codigoProducto
                comando.Parameters.Add("@codigoGuiaRemision", SqlDbType.Char, 8).Value = guiaRemision.codigoGuiaRemision
                comando.Parameters.Add("@cantidadProducto", SqlDbType.Int).Value = guiaRemision.DetalleGuia(i).cantidadProducto
                comando.Parameters.Add("@PesoKg", SqlDbType.Decimal).Value = guiaRemision.DetalleGuia(i).pesoKG

                If MyBase.ExecuteQuery(comando) > 0 Then
                    For j = 0 To guiaRemision.DetalleGuia(i).Series.Count - 1
                        'Insertar series
                        If guiaRemision.DetalleGuia(i).Series(j).DetalleExistencia.codigoDetalleExistencia > 0 Then
                            comando = MyBase.CreateSelectCommand("SPInsertSeriesGuia")

                            comando.Parameters.Add("@codigoDetalleExistencia", SqlDbType.Int).Value = guiaRemision.DetalleGuia(i).Series(j).DetalleExistencia.codigoDetalleExistencia
                            comando.Parameters.Add("@codigoProducto", SqlDbType.Char, 4).Value = guiaRemision.DetalleGuia(i).Producto.codigoProducto
                            comando.Parameters.Add("@codigoGuiaRemision", SqlDbType.Char, 8).Value = guiaRemision.codigoGuiaRemision


                            If MyBase.ExecuteQuery(comando) > 0 Then
                                'si hay falla rollback

                            End If
                        End If

                    Next
                End If
            Next
        End If

        Return guiaRemision.codigoGuiaRemision

    End Function

    'Public Function GetByIdFromGuiaRemision(ByVal codigoGuiaRemision As Integer) As GuiaRemision
    '    Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetByIdFromGuiaRemision")
    '    comando.Parameters.Add("@codigoGuiaRemision", SqlDbType.Int).Value = codigoGuiaRemision
    '    Return SelectObjectFactory(comando).Item(0)
    'End Function
    'Public Function GetAllFromGuiaRemision() As List(Of GuiaRemision)
    '    Dim comando As SqlCommand = MyBase.CreateSelectCommand("GetAllFromGuiaRemision")
    '    Return SelectObjectFactory(comando)
    'End Function

    'Private Function SelectObjectFactory(ByVal comando As SqlCommand) As List(Of GuiaRemision)
    '    Dim Lista As New List(Of GuiaRemision)
    '    Dim cmddetale As SqlCommand
    '    Dim cmdseries As SqlCommand
    '    Dim tGuiaRemision As New DataTable
    '    tGuiaRemision.TableName = "GuiaRemision"
    '    tGuiaRemision.Load(MyBase.ExecuteReader(comando))
    '    For Each Fila As DataRow In tGuiaRemision.Rows
    '        Dim GuiaRemision As New GuiaRemision()
    '        With GuiaRemision
    '            .codigoGuiaRemision = Fila.Item(0)
    '            .tipoGuiaRemision = Fila.Item(1)
    '            .numeroGuiaRemision = Fila.Item(2)
    '            .fechaEmision = Fila.Item(3)
    '            .origenEntrada = Fila.Item(4)
    '            .destinoSalida = Fila.Item(5)
    '            .descripcionNota = Fila.Item(6)
    '            .Almacen.codigoAlmacen = Fila.Item(7)
    '            .codigoUsuario = Fila.Item(8)
    '            .estadoGuiaRemision = Fila.Item(9)
    '            .Almacen.serieAlmacen = Fila.Item(10)

    '            'buscamos el detalle correspondiente a esta nota
    '            cmddetale = MyBase.CreateSelectCommand("GetByIdFromDetalleGuiaRemision")
    '            cmddetale.Parameters.Add("@codigoGuiaRemision", SqlDbType.Int).Value = .codigoGuiaRemision
    '            Dim tDetalleGuiaRemision As New DataTable
    '            tDetalleGuiaRemision.TableName = "DetalleGuiaRemision"
    '            tDetalleGuiaRemision.Load(MyBase.ExecuteReader(cmddetale))
    '            For Each FilaD As DataRow In tDetalleGuiaRemision.Rows
    '                Dim DetalleNota As New GuiaRemision.DetalleGuiaRemision
    '                DetalleNota.Producto.codigoProducto = FilaD.Item(1)
    '                DetalleNota.cantidadGuiaRemision = FilaD.Item(2)
    '                DetalleNota.Producto.codigoCategoria = FilaD.Item(3)
    '                DetalleNota.Producto.nombreProducto = FilaD.Item(4)
    '                DetalleNota.Producto.descripcionProducto = FilaD.Item(5)
    '                GuiaRemision.DetalleGui.Add(DetalleNota)

    '                'buscamos las series para este detalle
    '                cmdseries = MyBase.CreateSelectCommand("GetByIdFromSeriesGuiaRemision")
    '                cmdseries.Parameters.Add("@codigoGuiaRemision", SqlDbType.Int).Value = .codigoGuiaRemision
    '                cmdseries.Parameters.Add("@codigoProducto", SqlDbType.Char, 4).Value = DetalleNota.Producto.codigoProducto
    '                Dim tSeriesGuiaRemision As New DataTable
    '                tSeriesGuiaRemision.TableName = "SeriesGuiaRemision"
    '                tSeriesGuiaRemision.Load(MyBase.ExecuteReader(cmdseries))
    '                For Each FilaS As DataRow In tSeriesGuiaRemision.Rows
    '                    Dim SeriesNota As New GuiaRemision.DetalleGuiaRemision.SerieProductoGuiaRemision
    '                    SeriesNota.DetalleExistencia.codigoDetalleExistencia = FilaS.Item(0)
    '                    SeriesNota.DetalleExistencia.serieProducto = FilaS.Item(1)
    '                    SeriesNota.DetalleExistencia.estadoProducto = FilaS.Item(2)
    '                    SeriesNota.DetalleExistencia.estadoProductoAlmacen = FilaS.Item(3)
    '                    GuiaRemision.DetalleGui(GuiaRemision.DetalleGui.Count - 1).Series.Add(SeriesNota)
    '                    SeriesNota = Nothing
    '                Next
    '            Next
    '        End With
    '        Lista.Add(GuiaRemision)
    '        GuiaRemision = Nothing
    '    Next


    '    Return Lista
    'End Function
End Class
